package com.canbot.u05.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.canbot.u05.R;
import com.canbot.u05.bean.PresenterTask;

import java.util.ArrayList;
import java.util.List;

/**
 * 主持人模式首页模块适配器
 * Created by XH on 2017/3/3.
 */

public class CompereAdapter extends BaseAdapter {

        private List<PresenterTask> objects = new ArrayList<>();

        LayoutInflater inflater;

        Context mContext;

        public CompereAdapter(Context mContext, List<PresenterTask> objects) {
                this.objects = objects;
                this.mContext = mContext;
                inflater = LayoutInflater.from(mContext);
        }

        @Override
        public int getCount() {
                return objects == null ? 0 : objects.size();
        }

        @Override
        public PresenterTask getItem(int i) {
                return objects.get(i);
        }

        @Override
        public long getItemId(int i) {
                return i;
        }

        @Override
        public View getView(int i, View convertView, ViewGroup viewGroup) {
                if (convertView == null) {
                        convertView = inflater.inflate(R.layout.item_compere, null);
                        convertView.setTag(new ViewHolder(convertView));
                }
                initalizeViews(getItem(i), (ViewHolder) convertView.getTag());
                return convertView;
        }

        private void initalizeViews(PresenterTask object, ViewHolder holder) {

                int id = object.getId();
                String title = object.getTaskName();
                //添加数据
                holder.title.setText(title + "");
                int imageId = this.mContext.getResources().getIdentifier("man1" + (id % 6 + 1), "drawable", "com.canbot.u05");
                //holder.man.setImageResource(R.drawable.man11);
                holder.man.setImageResource(imageId);
                int bgId = this.mContext.getResources().getIdentifier("bg_content0" + (id % 6 + 1), "drawable", "com.canbot.u05");
                holder.bg.setBackgroundResource(bgId);
        }

        protected class ViewHolder {

                private TextView title;

                private RelativeLayout bg;

                private ImageView man;

                public ViewHolder(View view) {
                        title = (TextView) view.findViewById(R.id.tv_title01);
                        bg = (RelativeLayout) view.findViewById(R.id.rl_bg);
                        man = (ImageView) view.findViewById(R.id.iv_man11);
                }
        }
   /* public ViewHolder holder;
    List<CompereData> mData;
    LayoutInflater inflater;
    public Context mContext;

    public CompereAdapter(Context context, List<CompereData> mData) {
        super();
        this.mData = mData;
        inflater = LayoutInflater.from(context);
        this.mContext = context;

    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int i) {
        return mData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return mData.size();
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
    if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_compere, null);
            holder.title = (TextView) convertView.findViewById(R.id.tv_title01);
            holder.bg = (RelativeLayout) convertView.findViewById(R.id.rl_bg);
            holder.man = (ImageView) convertView.findViewById(R.id.iv_man11);
            convertView.setTag(holder);
       } else {
          holder = (ViewHolder) convertView.getTag();
      }
        CompereData data = mData.get(i);
        int id = data.getId();
        String title = data.getTitle();
        //添加数据
        holder.title.setText(title + "");
        //holder.man.setImageBitmap(BitmapFactory.decodeResource());
        //  holder.man.setImageResource(R.drawable.+"");
        holder.man.setImageResource(R.drawable.man11);


        holder.bg.setBackgroundResource(bgId);
        return convertView;
    }

    static class ViewHolder {
        TextView title;
        RelativeLayout bg;
        ImageView man;

    }*/
}
